feat: add group message flow context mode#8243
Open
Tsukumi233 wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- In
LongTermMemory._json_safeyou callisinstance(value, list | tuple), butlist | tuplecreates atypes.UnionTypewhichisinstancedoes not accept and will raise aTypeError; use a tuple of types instead (e.g.,isinstance(value, (list, tuple))).
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `LongTermMemory._json_safe` you call `isinstance(value, list | tuple)`, but `list | tuple` creates a `types.UnionType` which `isinstance` does not accept and will raise a `TypeError`; use a tuple of types instead (e.g., `isinstance(value, (list, tuple))`).
## Individual Comments
### Comment 1
<location path="astrbot/builtin_stars/astrbot/long_term_memory.py" line_range="239" />
<code_context>
+ return await self._json_safe(value.toDict())
+ if isinstance(value, dict):
+ return {k: await self._json_safe(v) for k, v in value.items()}
+ if isinstance(value, list | tuple):
+ return [await self._json_safe(item) for item in value]
+ return value
</code_context>
<issue_to_address>
**issue (bug_risk):** Using `isinstance(value, list | tuple)` will raise at runtime; `isinstance` does not accept `|` unions here.
`isinstance` requires a type or a tuple of types as its second argument; `list | tuple` (PEP 604 union) will raise `TypeError` at runtime, so this branch will fail for lists/tuples. Use `isinstance(value, (list, tuple))`, or `collections.abc.Sequence` if you need broader sequence support.
</issue_to_address>
### Comment 2
<location path="astrbot/builtin_stars/astrbot/long_term_memory.py" line_range="224-225" />
<code_context>
+ return content
+
+ async def _component_to_json_safe_dict(self, comp) -> dict:
+ if hasattr(comp, "to_dict"):
+ data = await comp.to_dict()
+ elif hasattr(comp, "toDict"):
+ data = comp.toDict()
</code_context>
<issue_to_address>
**issue (bug_risk):** Blindly awaiting `comp.to_dict()` assumes it is async; if it is sync this will fail at runtime.
Both `_component_to_json_safe_dict` and `_json_safe` currently assume any `to_dict` is async. If `to_dict` is a regular sync method returning a dict (a common case), `await comp.to_dict()` will raise `TypeError: object dict can't be used in 'await' expression`. Consider checking whether `to_dict` is awaitable (e.g. via `inspect.iscoroutinefunction` / `inspect.isawaitable`) and only `await` it when appropriate, otherwise call it synchronously.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a new 'flow' mode for group chat context management, which persists group messages and uses conversation cursors to provide a more stable long-term memory compared to the traditional sliding window. The implementation includes new database models for message records and cursors, a dedicated GroupMessageFlowManager, and updates to the configuration and core lifecycle to support these features. I have no feedback to provide.
aee15ee to
91791db
Compare
91791db to
f5e1d09
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
为群聊上下文感知新增
flow模式,让现代长上下文模型可以按群聊消息流增量接收上下文,而不是每次只依赖滑动窗口。默认仍为sliding_window,现有用户不启用新模式时行为保持不变。Modifications / 改动点
GroupMessageFlowRecord和GroupMessageFlowCursor数据表,用于持久化群聊消息流以及每个 conversation 的消费游标。group_context_mode,支持sliding_window与flow两种模式;flow模式会在 LLM 请求前注入<group_messages_delta>,并排除当前触发消息,避免重复上下文。group_flow_max_delta_messages每次只注入最近 N 条消息,默认 200;group_flow_max_message_chars每条消息只保留前 N 个字符,默认 1000;两者均可设为 0 关闭对应限制。/reset和/new等会话清理路径:清理后将 flow 游标推进到最新消息,使新会话不会带入旧 delta。group_flow_record_bot_messages仅控制命令或插件产生的普通机器人消息。Reply内嵌Plain等嵌套组件,并兼容同步/异步to_dict(),避免 flow 落库时报not JSON serializable。flow/sliding_window选项标签。Screenshots or Test Results / 运行截图或测试结果
验证步骤:
结果:
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。